//
//  YLProductInvestmentTableViewCell.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/29.
//  Copyright © 2017年 she1323. All rights reserved.
//  投资金额


#import "YLProductInvestmentTableViewCell.h"
#import "YLGlobeHeader.h"
#import "YLProductModel.h"
@interface YLProductInvestmentTableViewCell ()<UITextFieldDelegate>

/**
 预期收益
 */
@property (weak, nonatomic) IBOutlet UILabel *expectedRevenueLabel;
/**
 提示文字
 */
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
/**
 金额
 */
@property (weak, nonatomic) IBOutlet UITextField *moneyTextField;

@property (nonatomic, assign) NSUInteger beishu;

@property (nonatomic, assign) NSUInteger sum;

@property (weak, nonatomic) IBOutlet UIView *cycleBaseView;

@end


@implementation YLProductInvestmentTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
   
    self.beishu = 10000;
    
    _moneyTextField.delegate = self;
    
    
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSInteger sum = textField.text.integerValue;
    if (sum <= self.model.leastMoney) {
        self.sum = self.model.leastMoney / 1;
    }
//    } else if (sum > self.model.indexLimit) {
//        self.sum = self.model.indexLimit;
//    }
    else {
        NSInteger num = sum / 10000;
        self.sum = num * 10000;
    }

}

- (void)setModel:(YLProductModel *)model
{
    _model = model;
    self.sum = model.leastMoney;
    
//    if (model.leastMoney > self.model.indexLimit) {
//        self.sum = self.model.indexLimit;
//    }
    
    _titleLabel.text = [NSString stringWithFormat:@"10000的整数倍追加,起投金额%.2f", model.leastMoney];
}



- (void)setSum:(NSUInteger)sum
{
    _sum = sum;
    
    self.model.investmentMoney = sum;
    
    _moneyTextField.text = [NSString stringWithFormat:@"%zd", sum];
    
    _expectedRevenueLabel.attributedText = [self attStringWithTitle:sum * ([_model.annualizedYield floatValue] / 100.0) * _model.productTerm / 12.0f];
    
}

- (NSMutableAttributedString *)attStringWithTitle:(CGFloat )number
{
    NSMutableAttributedString *att1 = [[NSMutableAttributedString alloc] initWithString:@"预期收益:"];
    
    [att1 appendAttributedString:[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"  %.2f  ", number] attributes:@{NSForegroundColorAttributeName : YLMainColor}]];
    
    [att1 appendAttributedString:[[NSMutableAttributedString alloc] initWithString:@"元"]];
    
    return att1;
}

// 减
- (IBAction)minus:(UIButton *)sender {
    if (self.sum <= self.model.leastMoney) {
        return;
    }
    self.sum = self.sum - _beishu;
}
// 加
- (IBAction)add:(UIButton *)sender {
    NSUInteger sum = self.sum + _beishu;
//    if (sum > self.model.indexLimit) {
//        self.sum = self.model.indexLimit;
//    } else {
        self.sum = sum;
//    }
}


+ (NSString *)cellID
{
    return @"YLProductInvestmentTableViewCell";
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

@end
